Related articles

*   [Arch Build System](/index.php/Arch_Build_System "Arch Build System")
*   [Arch packaging standards](/index.php/Arch_packaging_standards "Arch packaging standards")
*   [Arch User Repository](/index.php/Arch_User_Repository "Arch User Repository")
*   [Creating packages for other distributions](/index.php/Creating_packages_for_other_distributions "Creating packages for other distributions")
*   [makepkg](/index.php/Makepkg "Makepkg")
*   [pacman](/index.php/Pacman "Pacman")
*   [Patching in ABS](/index.php/Patching_in_ABS "Patching in ABS")
*   [PKGBUILD](/index.php/PKGBUILD "PKGBUILD")
*   [.SRCINFO](/index.php/.SRCINFO ".SRCINFO")
*   [DeveloperWiki:Building in a Clean Chroot](/index.php/DeveloperWiki:Building_in_a_Clean_Chroot "DeveloperWiki:Building in a Clean Chroot")

This article aims to assist users creating their own packages using the Arch Linux "ports-like" [build system](/index.php/Arch_Build_System "Arch Build System"), also for submission in [AUR](/index.php/AUR "AUR"). It covers creation of a [PKGBUILD](/index.php/PKGBUILD "PKGBUILD") – a package build description file sourced by `makepkg` to create a binary package from source. If already in possession of a `PKGBUILD`, see [makepkg](/index.php/Makepkg "Makepkg"). For instructions regarding existing rules and ways to improve package quality see [Arch packaging standards](/index.php/Arch_packaging_standards "Arch packaging standards").

## Contents

*   [1 Overview](#Overview)
    *   [1.1 Meta packages and groups](#Meta_packages_and_groups)
*   [2 Preparation](#Preparation)
    *   [2.1 Prerequisite software](#Prerequisite_software)
    *   [2.2 Download and test the installation](#Download_and_test_the_installation)
*   [3 Creating a PKGBUILD](#Creating_a_PKGBUILD)
    *   [3.1 Defining PKGBUILD variables](#Defining_PKGBUILD_variables)
    *   [3.2 PKGBUILD functions](#PKGBUILD_functions)
        *   [3.2.1 prepare()](#prepare.28.29)
        *   [3.2.2 pkgver()](#pkgver.28.29)
        *   [3.2.3 build()](#build.28.29)
        *   [3.2.4 check()](#check.28.29)
        *   [3.2.5 package()](#package.28.29)
*   [4 Testing the PKGBUILD and package](#Testing_the_PKGBUILD_and_package)
    *   [4.1 Checking package sanity](#Checking_package_sanity)
*   [5 Submitting packages to the AUR](#Submitting_packages_to_the_AUR)
*   [6 Summary](#Summary)
    *   [6.1 Warnings](#Warnings)
*   [7 More detailed guidelines](#More_detailed_guidelines)
*   [8 PKGBUILD generators](#PKGBUILD_generators)
*   [9 See also](#See_also)

## Overview

Packages in Arch Linux are built using the [makepkg](/index.php/Makepkg "Makepkg") utility and the information stored in a [PKGBUILD](/index.php/PKGBUILD "PKGBUILD") file. When `makepkg` runs, it searches for a `PKGBUILD` in the current directory and follows the instructions in it to acquire the required files and/or compile them to be packed within a package file (`pkgname.pkg.tar.xz`). The resulting package contains binary files and installation instructions ready to be installed by [pacman](/index.php/Pacman "Pacman").

An Arch package is no more than a tar archive, or 'tarball', compressed using xz, which contains the following files generated by makepkg:

*   The binary files to install.
*   `.PKGINFO`: contains all the metadata needed by pacman to deal with packages, dependencies, etc.
*   `.BUILDINFO`: contains information needed for reproducible builds. This file is present only if a package is built with pacman 5.1 or newer.
*   `.MTREE`: contains hashes and timestamps of the files, which are included in the local database so that pacman can verify the integrity of the package.
*   `.INSTALL`: an optional file used to execute commands after the install/upgrade/remove stage. (This file is present only if specified in the `PKGBUILD`.)
*   `.Changelog`: an optional file kept by the package maintainer documenting the changes of the package. (It is not present in all packages.)

### Meta packages and groups

A package group is a set of related packages, defined by the packager, which can be installed or uninstalled simultaneously by using the group name as a substitute for each individual package name. While a group is not a package, it can be installed in a similar fashion to a package (see [Pacman#Installing package groups](/index.php/Pacman#Installing_package_groups "Pacman") and [PKGBUILD#groups](/index.php/PKGBUILD#groups "PKGBUILD")).

A meta package, often (though not always) titled with the *-meta* suffix, provides similar functionality to a package group in that it enables multiple related packages to be installed or uninstalled simultaneously. Meta packages can be installed just like any other package (see [Pacman#Installing specific packages](/index.php/Pacman#Installing_specific_packages "Pacman")). The only difference between a meta package and a regular package is that a meta package is empty and exists purely to link related packages together via dependencies.

The advantage of a meta package, compared to a group, is that any new member packages will be installed when the meta package itself is updated with a new set of dependencies. This is in contrast to a group where new group members will not be automatically installed. The disadvantage of a meta package is that it is not as flexible as a group; you can choose which group members you wish to install but you cannot choose which meta package dependencies you wish to install. Likewise, you can uninstall group members without having to remove the entire group. However, you cannot remove meta package dependencies without having to uninstall the meta package itself.

## Preparation

### Prerequisite software

First, ensure that the necessary tools are [installed](/index.php/Install "Install"): the package group [base-devel](https://www.archlinux.org/groups/x86_64/base-devel/) should be sufficient, it includes **make** and additional tools needed for compiling from source.

The key tool for building packages is [makepkg](/index.php/Makepkg "Makepkg") (provided by [pacman](https://www.archlinux.org/packages/?name=pacman)), which does the following:

1.  Checks if package dependencies are installed.
2.  Downloads the source file(s) from the specified server(s).
3.  Unpacks the source file(s).
4.  Compiles the software and installs it under a fakeroot environment.
5.  Strips symbols from binaries and libraries.
6.  Generates the package meta file which is included with each package.
7.  Compresses the fakeroot environment into a package file.
8.  Stores the package file in the configured destination directory, which is the present working directory by default.

### Download and test the installation

Download the source tarball of the software you want to package, extract it, and follow the author's steps to install the program. Make a note of all commands and/or steps needed to compile and install it. You will be repeating those same commands in the `PKGBUILD` file.

Most software authors stick to the 3-step build cycle:

```
./configure
make
make install

```

This is a good time to make sure the program is working correctly.

## Creating a PKGBUILD

When `makepkg` is run, it looks for a `PKGBUILD` file in the present working directory. If it finds one, it downloads the software's source code and compile it according to the instructions specified in the `PKGBUILD` file. The instructions must be fully interpretable by the [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell) shell. After successful completion, the resulting binaries and metadata of the package, i.e. package version and dependencies, are packed in a `pkgname.pkg.tar.xz` package file. The newly created package can be installed by simply using `makepkg --install` which will call pacman in the background, or by directly using `pacman -U *pkgname.pkg.tar.xz*`.

To start building a new package, first create a new directory for the package and change current directory into this one. Then, a `PKGBUILD` file needs to be created: a prototype PKGBUILD found in `/usr/share/pacman/` can be used or you can start from a `PKGBUILD` from another package. The latter may be a good choice if a similar package already exists.

### Defining PKGBUILD variables

Example PKGBUILDs are located in `/usr/share/pacman/`. An explanation of possible `PKGBUILD` variables can be found in the [PKGBUILD](/index.php/PKGBUILD "PKGBUILD") article.

*makepkg* defines two variables that you should use as part of the build and install process:

	`srcdir`

	This points to the directory where *makepkg* extracts or symlinks all files in the source array.

	`pkgdir`

	This points to the directory where *makepkg* bundles the installed package, which becomes the root directory of your built package.

They contain *absolute* paths, which means you do not have to worry about your working directory if you use these variables properly.

**Note:** *makepkg*, and thus the `build()` and `package()` functions, are intended to be non-interactive. Interactive utilities or scripts called in those functions may break *makepkg*, particularly if it is invoked with build-logging enabled (`-L`). (See [FS#13214](https://bugs.archlinux.org/task/13214).)

### PKGBUILD functions

There are five functions, listed here in the order they are executed. Beside the fifth function, `package()`, which is required in every PKGBUILD, if one function does not exist it is simply skipped.

#### prepare()

This function, commands that are used to prepare sources for building are run, such as [patching](/index.php/Patching_in_ABS "Patching in ABS"). This function runs right after package extraction, before [pkgver()](#pkgver.28.29) and the build function. If extraction is skipped (`makepkg -e`), then `prepare()` is not run.

**Note:** (From [PKGBUILD(5)](https://jlk.fjfi.cvut.cz/arch/manpages/man/PKGBUILD.5)) The function is run in `bash -e` mode, meaning any command that exits with a non-zero status will cause the function to exit.

#### pkgver()

`pkgver()` runs after the sources are fetched, extracted and [prepare()](#prepare.28.29) executed. So you can update the pkgver variable during a makepkg stage.

This is particularly useful if you are [making git/svn/hg/etc. packages](/index.php/VCS_PKGBUILD_Guidelines "VCS PKGBUILD Guidelines"), where the build process may remain the same, but the source could be updated every day, even every hour. The old way of doing this was to put the date into the pkgver field which, if the software was not updated, makepkg would still rebuild it thinking the version had changed. Some useful commands for this are `git describe`, `hg identify -ni`, etc. Please test these before submitting a PKGBUILD, as a failure in the `pkgver()` function can stop a build in its tracks.

**Note:** pkgver cannot contain spaces or hyphens (`-`). Using sed to correct this is common.

#### build()

Now you need to implement the `build()` function in the `PKGBUILD` file. This function uses common shell commands in [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell) syntax to automatically compile software and create a `pkg` directory to install the software to. This allows *makepkg* to package files without having to sift through your file system.

The first step in the `build()` function is to change into the directory created by uncompressing the source tarball. *makepkg* will change the current directory to `$srcdir` before executing the `build()` function. Therefore, in most cases, like suggested in `/usr/share/pacman/PKGBUILD.proto`, the first command will look like this:

```
cd "$pkgname-$pkgver"

```

Now, you need to list the same commands you used when you manually compiled the software. The `build()` function in essence automates everything you did by hand and compiles the software in the fakeroot build environment. If the software you are packaging uses a configure script, it is good practice to use `--prefix=/usr` when building packages for pacman. A lot of software installs files relative to the `/usr/local` directory, which should only be done if you are manually building from source. All Arch Linux packages should use the `/usr` directory. As seen in the `/usr/share/pacman/PKGBUILD.proto` file, the next two lines often look like this:

```
./configure --prefix=/usr
make

```

**Note:** If your software does not need to build anything, do not use the `build()` function. The `build()` function is not required, but the `package()` function is.

#### check()

Place for calls to `make check` and similar testing routines. It is highly recommended to have `check()` as it helps to make sure software has been built correctly and works fine with its dependencies.

Users who do not need it (and occasionally maintainers who can not fix a package for this to pass) can disable it using `BUILDENV+=('!check')` in PKGBUILD/makepkg.conf or call `makepkg` with `--nocheck` flag.

#### package()

The final step is to put the compiled files in a directory where *makepkg* can retrieve them to create a package. This by default is the `pkg` directory—a simple fakeroot environment. The `pkg` directory replicates the hierarchy of the root file system of the software's installation paths. If you have to manually place files under the root of your filesystem, you should install them in the `pkg` directory under the same directory structure. For example, if you want to install a file to `/usr/bin`, it should instead be placed under `$pkgdir/usr/bin`. Very few install procedures require the user to copy dozens of files manually. Instead, for most software, calling `make install` will do so. The final line should look like the following in order to correctly install the software in the `pkg` directory:

```
make DESTDIR="$pkgdir/" install

```

**Note:** It is sometimes the case where `DESTDIR` is not used in the `Makefile`; you may need to use `prefix` instead. If the package is built with *autoconf* / *automake*, use `DESTDIR`; this is what is [documented](https://www.gnu.org/software/automake/manual/automake.html#Install) in the manuals. If `DESTDIR` does not work, try building with `make prefix="$pkgdir/usr/" install`. If that does not work, you will have to look further into the install commands that are executed by "`make <...> install`".

`makepkg --repackage` runs only the `package()` function, so it creates a package without building. This may save time e.g. if you have changed just the `depends` variable of the package.

## Testing the PKGBUILD and package

As you are writing the `build()` function, you will want to test your changes frequently to ensure there are no bugs. You can do this using the `makepkg` command in the directory containing the `PKGBUILD` file. With a properly formatted `PKGBUILD`, makepkg will create a package; with a broken or unfinished `PKGBUILD`, it will raise an error.

If makepkg finishes successfully, it will place a file named `pkgname-pkgver.pkg.tar.xz` in your working directory. This package can be installed with the `pacman -U` command. However, just because a package file was built does not imply that it is fully functional. It might conceivably contain only the directory and no files whatsoever if, for example, a prefix was specified improperly. You can use pacman's query functions to display a list of files contained in the package and the dependencies it requires with `pacman -Qlp [package file]` and `pacman -Qip [package file]` respectively.

If the package looks sane, then you are done! However, if you plan on releasing the `PKGBUILD` file, it is imperative that you check and double-check the contents of the `depends` array.

Also ensure that the package binaries actually *run* flawlessly! It is annoying to release a package that contains all necessary files, but crashes because of some obscure configuration option that does not quite work well with the rest of the system. If you are only going to compile packages for your own system, though, you do not need to worry too much about this quality assurance step, as you are the only person suffering from mistakes, after all.

### Checking package sanity

After testing package functionality check it for errors using [namcap](/index.php/Namcap "Namcap"):

```
$ namcap PKGBUILD
$ namcap *<package file name>*.pkg.tar.xz

```

Namcap will:

1.  Check PKGBUILD contents for common errors and package file hierarchy for unnecessary/misplaced files
2.  Scan all ELF files in package using `ldd`, automatically reporting which packages with required shared libraries are missing from `depends` and which can be omitted as transitive dependencies
3.  Heuristically search for missing and redundant dependencies

and much more. Get into the habit of checking your packages with namcap to avoid having to fix the simplest mistakes after package submission.

## Submitting packages to the AUR

Please read [AUR User Guidelines#Submitting packages](/index.php/AUR_User_Guidelines#Submitting_packages "AUR User Guidelines") for a detailed description of the submission process.

## Summary

1.  Download the source tarball of the software to package.
2.  Try compiling the package and installing it into an arbitrary directory.
3.  Copy over the prototype `/usr/share/pacman/PKGBUILD.proto` and rename it to `PKGBUILD` in a temporary working directory.
4.  Edit the `PKGBUILD` according to the needs of your package.
5.  Run `makepkg` and check whether the package builds correctly.
6.  If not, repeat the last two steps.

### Warnings

*   Before you can automate the package building process, you should have done it manually at least once unless you know *exactly* what you are doing *in advance*, in which case you would not be reading this in the first place. Unfortunately, although a good bunch of program authors stick to the 3-step build cycle of "`./configure`; `make`; `make install`", this is not always the case, and things can get real ugly if you have to apply patches to make everything work at all. Rule of thumb: If you cannot get the program to compile from the source tarball, and make it install itself to a defined, temporary subdirectory, you do not even need to try packaging it. There is not any magic pixie dust in `makepkg` that makes source problems go away.
*   In a few cases, the packages are not even available as source and you have to use something like `sh installer.run` to get it to work. You will have to do quite a bit of research (read READMEs, INSTALL instructions, man pages, perhaps ebuilds from Gentoo or other package installers, possibly even the MAKEFILEs or source code) to get it working. In some really bad cases, you have to edit the source files to get it to work at all. However, `makepkg` needs to be completely autonomous, with no user input. Therefore if you need to edit the makefiles, you may have to bundle a custom patch with the `PKGBUILD` and install it from inside the `prepare()` function, or you might have to issue some `sed` commands from inside the `prepare()` function.

## More detailed guidelines

**<a class="mw-selflink selflink">Package creation guidelines</a>**

* * *

[CLR](/index.php/CLR_package_guidelines "CLR package guidelines") – [Cross](/index.php/Cross-compiling_tools_package_guidelines "Cross-compiling tools package guidelines") – [Eclipse](/index.php/Eclipse_plugin_package_guidelines "Eclipse plugin package guidelines") – [Free Pascal](/index.php/Free_Pascal_package_guidelines "Free Pascal package guidelines") – [GNOME](/index.php/GNOME_package_guidelines "GNOME package guidelines") – [Go](/index.php/Go_package_guidelines "Go package guidelines") – [Haskell](/index.php/Haskell_package_guidelines "Haskell package guidelines") – [Java](/index.php/Java_package_guidelines "Java package guidelines") – [KDE](/index.php/KDE_package_guidelines "KDE package guidelines") – [Kernel](/index.php/Kernel_module_package_guidelines "Kernel module package guidelines") – [Lisp](/index.php/Lisp_package_guidelines "Lisp package guidelines") – [MinGW](/index.php/MinGW_package_guidelines "MinGW package guidelines") – [Node.js](/index.php/Node.js_package_guidelines "Node.js package guidelines") – [Nonfree](/index.php/Nonfree_applications_package_guidelines "Nonfree applications package guidelines") – [OCaml](/index.php/OCaml_package_guidelines "OCaml package guidelines") – [Perl](/index.php/Perl_package_guidelines "Perl package guidelines") – [PHP](/index.php/PHP_package_guidelines "PHP package guidelines") – [Python](/index.php/Python_package_guidelines "Python package guidelines") – [Ruby](/index.php/Ruby_Gem_package_guidelines "Ruby Gem package guidelines") – [VCS](/index.php/VCS_package_guidelines "VCS package guidelines") – [Web](/index.php/Web_application_package_guidelines "Web application package guidelines") – [Wine](/index.php/Wine_package_guidelines "Wine package guidelines")

## PKGBUILD generators

PKGBUILDs for some packages can be generated automatically.

**Note:** Users are still responsible for ensuring that the package meets the high quality standards before submitting the generated files to the [AUR](/index.php/AUR "AUR").

*   [Go](/index.php/Go "Go"): [go-makepkg](https://github.com/seletskiy/go-makepkg)
*   [Haskell](/index.php/Haskell "Haskell"): [cblrepo](https://github.com/magthe/cblrepo)
*   [Python](/index.php/Python "Python"): [pipman-git](https://aur.archlinux.org/packages/pipman-git/), [pip2arch-git](https://aur.archlinux.org/packages/pip2arch-git/), [python-pypi2pkgbuild](https://aur.archlinux.org/packages/python-pypi2pkgbuild/)
*   [Ruby](/index.php/Ruby "Ruby"): [gem2arch](https://aur.archlinux.org/packages/gem2arch/), [pacgem](https://aur.archlinux.org/packages/pacgem/)

## See also

*   [How to correctly create a patch file](https://bbs.archlinux.org/viewtopic.php?id=91408).
*   [Arch Linux Classroom IRC Logs of classes about creating PKGBUILDs](https://archwomen.org/media/project_classroom/classlogs/).
*   [Fakeroot approach for package installation](http://www.linuxfromscratch.org/hints/downloads/files/fakeroot.txt)